(function ($, Drupal, window, document, undefined) {
    Drupal.behaviors.my_custom_behavior = {
        attach: function (context, settings) {
			// auto upload file on file field change
			if ($.browser.msie){
				$('form input.form-file').attr('onchange', '_p(this)');
			}else{
				$('form', context).delegate('input.form-file', 'change', function(e) {
					$(this).next('input[type="submit"]').mousedown();
				});
			}

			// use "chosen" for select
/* 			$('select').on('chosen:ready', function(evt, params){
				// restore width to "inherit" if the calc width is 0 ( due to element hidden )
				// make sure this listener is before chosen init
				if(params.chosen.container.width() == 0){
					params.chosen.container.width('inherit');
				}
			});
			$('select').on('change', function(evt, params){
				if(params.selected == '_none'){
					if(evt.target.multiple == true){
						var list = new Array();
						$(this).children('option').each(function(){
							if($(this).val() != '_none'){
								list.push($(this).val());
							}
						});
						$(this).val(list);
						$(this).trigger("chosen:updated");
					}
				}
			});
			$('#edit-field-business-unit-single-und').on('chosen:hiding_dropdown', function(evt, params){
				// use click event to trigger ajax event since there may only have one option
				$(this).trigger("click");
			});
			$('select').each(function(){
				if(typeof $(this).attr('data-limit') != "undefined" && $(this).attr('data-limit') == '1'){
					$(this).removeAttr('multiple');
					$(this).chosen({max_selected_options: 1});
				}else if(typeof $(this).attr('multiple') == "undefined"){
					$(this).chosen({max_selected_options: 1});
				}else{
					if ($(this).attr('id') != 'edit-field-gaamp-request-type-und'){
						$(this).children('option').each(function(){
							if ($(this).val() == '_none'){
								$(this).text('- All - ');
							}
						});
						$(this).chosen();
					}
				}
				$(this).trigger("chosen:updated");
			}); */



			// icheck
            var $body = $('body');
            if (!$body.hasClass('page-stylebook') && !$body.hasClass('page-msw')) {
                $('input').iCheck({
                    checkboxClass: 'icheckbox',
                    radioClass: 'iradio',
                });
            }
			$('input').on('ifToggled', function(event){
				$(this).change();
			});

		}
    };

    var page_width = $(window).width();
    var page_height = $(window).height();


    $(document).ready(function () {
        //Placeholder & its Compability to ie8
        $('form input[type=text], form input[type=email], form input[type=password], form textarea').each(function () {
            var input = $(this);
            if (input.is('.block-search .form-item-search-block-form input.form-text') || input.is('.region-help .views-widget-filter-combine .form-text')) {
                input.attr("placeholder", "Search keywords...");
            }
        });
        if ($.browser.msie && $.browser.version <= 9) {
            $("[placeholder]").focus(function () {
                if ($(this).val() == $(this).attr("placeholder") || $(this).parent().hasClass("placeholder")) {
                    $(this).val("");

                    if ($(this).hasClass("password-field") || $(this).hasClass("password-confirm")) {
                        $(this).parent().removeClass("placeholder");
                    }
                }
            }).blur(function () {
                if ($(this).val() == "") {
                    $(this).val($(this).attr("placeholder"));

                    if ($(this).hasClass("password-field") || $(this).hasClass("password-confirm")) {
                        $(this).parent().addClass("placeholder");
                        $(this).val("");
                    }
                }
            }).blur();

            $("[placeholder]").parents("form").submit(function () {
                $(this).find('[placeholder]').each(function () {
                    if ($(this).val() == $(this).attr("placeholder")) {
                        $(this).val("");
                    }
                })
            });
        }


        // Search block - Override default one to views one

//        $('.block-search form').submit(function(e) {
//            e.preventDefault();
//
//            var str = $(".block-search form").find("input.form-text").val();
//            var words = str.split(" ");
//            var output = "";
//            for (var i = 0; i < words.length; i++) {
//                output += "+" + words[i];
//            }
//            window.location.href = "/search?combine=" + output;
//        })

        // full width background image
        // Gordon: Who write these...?
//		var bgimage = $("div.fluid-image").attr('bgimage');
//		if ($(bgimage).attr('src')) {
//		  $("div.fluid-image").css("background", 'url("http://jessicadev.xenyo.net/sites/default/files/promotion_a.jpg");');
//		  console.log($("div.fluid-image"));
//		}

        // Fluid image BG STYLE

        $(".fluid-image[bgimage]").each(function() {

            if ($(this).next(".image-path").length >= 1) {
                var url = "url(" + $(this).next(".image-path").find("img").attr("src") + ")";
            } else {
                var url = "url(" + $(this).attr("bgimage") + ")";
            }
            $(this).attr("style","background-image: " + url).attr("bgimage","");;
        })

        // Show/hide header search bar

//        var $searchForm = $('#views-exposed-form-search-page-page');
//        var $searchFormInputField = $('#views-exposed-form-search-page-page input#edit-keys');
//        var $searchLabel = $('.header__region .menu__item.last .menu__link.nolink');
//        var $body = $('body');
//        var hideSearchForm = function () {
//        	$searchForm.hide();
//        };
//
//	    $searchLabel.click(function (e) {
//	    	e.stopPropagation();
//	    	$searchForm.toggle();
//	    	$searchForm.animate({width: "350px"}, 500);
//	    	$searchFormInputField.focus();
//	    });
//
//	    $searchForm.click(function (e) {
//	    	e.stopPropagation();
//	    });
//
//		$body.click(function () {
//			$searchForm.animate({width: "50px"}, 500, 'swing', hideSearchForm);
//		});

        //lotfy xmass function
		$("body.desktop.section-christmas-2015 #page #main #content .wrapper .jessica_xmas_subcat_main_ad").insertAfter("body.desktop.section-christmas-2015 #page #main #content .wrapper .jessica_xmas_subcat_views .wrapper .view-content .views-row-2");




        $("form[action='/search-page']").addClass("search-block").appendTo(".region-header ul.menu");

        $(".region-header .menu__link.search").click(function() {
//            $("form.search-block").toggleClass("toggled");
            $("form.search-block").fadeToggle(500, function() {
                $(this).find(".form-text").focus();
            });
        })
        $("form.search-block .form-text").blur(function() {
//            $("form.search-block").removeClass("toggled")
            $("form.search-block").fadeOut(500);
        });
        $("form.search-block .form-text").attr("placeholder", "SEARCH...");

        // Close Quote of the Day

        $(".popup.quote-of-the-day .close").click(function() {
            $(this).parents(".popup").fadeOut();
        })

    });

    $(window).load(function () {

        $(".messages").dblclick(function () {
            if (!$(".krumo-root").length) {
                $(this).fadeOut(500);
            }
        })

        //Sticky Header

        var $sticky_object = $("#header");
        var admin_menu_height = 0;
        if ($("body.admin-menu").length) {
            admin_menu_height = 29;
        }
        var $wrapper = $("<div>").addClass("sticky-wrapper").height($sticky_object.outerHeight(true) /*+ admin_menu_height*/);

//		if (!$("body.front").length) {
////			$sticky_object.wrap($wrapper).addClass("sticky");
////			$sticky_object.addClass("sticky");
//            $("#header").waypoint('sticky',{
//                wrapper:'<div class="sticky-wrapper" />',
//                stuckClass:'sticky',
//                offset: 0,
//            })
//		}
        $sticky_object.wrap($wrapper);

		$(".front .region-highlighted, .not-front #main").waypoint(function(direction) {
            $("body").toggleClass("mini-menu");
			$sticky_object.toggleClass("minimized");
		}, {
			//			offset: '-100px',
		});




        // Popup block

		var $popup_btn = $('.popup-btn');

		$popup_btn.on('click', function(event) {

			event.preventDefault();
			$('div.popup-overlay .popup').hide();
			$('div.popup-overlay').fadeIn(250);
			$('body').addClass("popup-on");

			 if ($(this).is("#masquerade")) {
			 	$('.popup.block-masquerade').fadeIn(250);
			 }
		});

		var mouse_is_inside = false;

		$('.popup').hover(function(){
			mouse_is_inside = true;
		}, function(){
			mouse_is_inside = false;
		});

		$("div.popup-overlay").click(function(){
			if(! mouse_is_inside && $("body.popup-on").length) {
				$('.popup, div.popup-overlay').fadeOut(250);
				$('body').removeClass("popup-on");
			}
		});




        // Adding Placeholder for non-label form items

        $("form label").each(function() {

            if($(this).css("display") == "none") {
                var $text = $(this).text();
                $(this).parent(".form-item").find(".form-text, .form-pass").attr("placeholder",$text);
            }
        })

    })
    $(window).resize(function () {

    });

})(jQuery, Drupal, this, this.document);


function _p(elem){
	if ($(elem).val().length > 0){
		$(elem).next('input[type="submit"]').mousedown();
	}
}
